package org.xlx.xlx_oa.role_user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.role_user.convertor.RoleUserConvertor;
import org.xlx.xlx_oa.role_user.domain.AdmPosition;
import org.xlx.xlx_oa.role_user.domain.AdmUser;
import org.xlx.xlx_oa.role_user.domain.dto.AdmPositionDto;
import org.xlx.xlx_oa.role_user.domain.vo.AdmPositionVo;
import org.xlx.xlx_oa.role_user.mapper.AdmPositionMapper;

import java.util.List;
import java.util.Objects;

@Service
public class AdmPositionService {
    private final RoleUserConvertor roleUserConvertor = RoleUserConvertor.INSTANCE;
    @Autowired
    private AdmPositionMapper positionMapper;
    @Autowired
    private AdmUserService userService;

    /**
     * 保存
     */
    public Boolean saveEntity(AdmPositionVo positionVo) {
        AdmPosition position = roleUserConvertor.convertVoToPosition(positionVo);
        Long id = position.getId();
        if (Objects.isNull(id)) {
            positionMapper.insert(position);
        } else {
            positionMapper.updateById(position);
        }
        return true;
    }

    /**
     * 删除
     */
    public Boolean removeEntity(Long id) {
        AdmUser user = new AdmUser();
        user.setPositionId(id);
        Boolean empty = userService.checkDepartOrPositionEmpty(user);
        if (!empty) {
            throw new BusinessException("100008", "Still in use, please empty it first");
        }
        return positionMapper.deleteById(id) > 0;
    }

    /**
     * 列表
     */
    public List<AdmPositionDto> getList(AdmPositionVo positionVo) {
        AdmPosition position = roleUserConvertor.convertVoToPosition(positionVo);
        Page<AdmPosition> page = new Page<>(positionVo.getPageNum(), positionVo.getPageSize());
        QueryWrapper<AdmPosition> queryWrapper = new QueryWrapper<>();
        if (!Objects.isNull(position.getTitle())) {
            queryWrapper.lambda().like(AdmPosition::getTitle, position.getTitle());
        }
        positionMapper.selectPage(page, queryWrapper);
        List<AdmPositionDto> positionDtos = roleUserConvertor.convertToListAdmPositionDto(page.getRecords());
        return positionDtos;
    }

    /**
     * 数据总个数
     *
     * @return
     */
    public Integer getCount(AdmPositionVo positionVo) {
        AdmPosition position = roleUserConvertor.convertVoToPosition(positionVo);
        QueryWrapper<AdmPosition> queryWrapper = new QueryWrapper<>();
        if (!Objects.isNull(position.getTitle())) {
            queryWrapper.lambda().like(AdmPosition::getTitle, position.getTitle());
        }
        return positionMapper.selectCount(queryWrapper);
    }

    /**
     * 详情
     */
    public AdmPositionDto getDetail(Long id) {
        AdmPosition position = positionMapper.selectById(id);
        return roleUserConvertor.convertToAdmPositionDto(position);
    }
}
